package com.withoutwords.engine;

import com.withoutwords.R;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public final class Common {
	public static final String responseCode = "responseCode";
	public static final String roundCode = "roundInfo";
	public static final String userIdCode = "userId";
	public static final String gameIdCode = "gameId";
	public static final String usersCode = "users";
	public static final String roundNoCode = "roundNo";
	public static final String LastActingData = "lastActingData";
	public static final String ownerIdCode = "ownerId";
	public static final String gamesCode = "games";
	public static final String winner = "winner";
	public static final String fbLoginSuccess = "fbLoginSuccess";
	public static final String gameJoined = "gamejoined";
	public static final String approvedGuessCode = "approvedGuess";
	public static final String players = "players";
	public static final String openHelp = "openHelp";
	

	public static final String drawingBytes = "Drawing";
	public static final String wordIndex = "wordIndex";
	public static final String gameCreatorId = "creatorId";
	public static final String gameCreatorName = "creatorName";
	//public static final String unreadGuessList = "unreadGuessList";

	
	public static final String notificationId = "notificationId";

	public static final int guessRecievedNotificatoin = 234;
	public static final int guessApprovedNotificatoin = 235;
	public static final int actingRecievedNotificatoin = 236;
	public static final int invitationRecievedNotificatoin = 237;
	public static final int roundEndedNotificatoin = 238;

	
	
	private static final int baseScreenwidth = 800;
	public static void showNotification(int notId, Context context,
			Intent intent, String title, String tickerText, String text) {
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

		// prepare notification pending intent
		PendingIntent contentIntent = null;

		contentIntent = PendingIntent.getActivity(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		// prepare notification
		Notification notification = new Notification();

		notification = new Notification(R.drawable.icon_notification, // the icon for the
															// status bar
				tickerText, // the text to display in the ticker
				System.currentTimeMillis()); // the time stamp for the
												// notification

		// prepare notification details
		notification.setLatestEventInfo(context, // the context to use
				title,
				// the title for the notification
				text, // the details to display in the notification
				contentIntent); // the contentIntent (see above)

		// set notification sound

		notification.defaults |= Notification.DEFAULT_SOUND;

		notification.defaults |= Notification.DEFAULT_VIBRATE;

		// show notification
		notificationManager.notify(notId, // we use a string id
											// because it is a
											// unique
				// number. we use it later to cancel the notification
				notification);

	}

	/**
	 * cancel notification by id
	 * 
	 * @param context
	 *            system context
	 * @param id
	 *            notification id
	 */
	public static void cancelNotification(Context context, int id) {
		if (context != null) {
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			if (notificationManager != null)
				notificationManager.cancel(id);
		}
	}

	/**
	 * cancel All Notifications
	 * 
	 * @param context
	 * @param id
	 */
	public static void cancelAllNotification(Context context) {
		if (context != null) {
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			if (notificationManager != null)
				notificationManager.cancelAll();
		}
	}

	static Typeface tf = null;

	public static Typeface getTypeface(Context context) {

		if (tf == null) {
			tf = Typeface.createFromAsset(context.getAssets(),
					"fonts/Frank_choco_cookie.ttf");
		}
		return tf;
	}
	
	/*
	 * This function adjusts the fixed Text sizes according to device denisty and width
	 */
	public static float adjustTextSize(float originalSize,Context context)
	{
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		//if(metrics.density = metrics.DENSITY_MEDIUM && metrics.)
		
		float newSize = (originalSize / metrics.scaledDensity)*((float)metrics.widthPixels/baseScreenwidth);
		return newSize;
	}
	/*
	 * This function adjusts the fixed sizes according to device denisty and width
	 */
	public static float adjustSize(float originalSize,Context context)
	{
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		//if(metrics.density = metrics.DENSITY_MEDIUM && metrics.)
		
		float newSize = (originalSize)*((float)metrics.widthPixels/baseScreenwidth);
		return newSize;
	}
	public static void showPlayersLeftDialog(Context c,boolean bIsGameOwnerLeft ,DialogInterface.OnClickListener positiveListener)
	{
		String dialogMessage = "";
		if(bIsGameOwnerLeft)
		{
			dialogMessage = c.getString(R.string.creator_player_left);
		}
		else
		{
			dialogMessage = c.getString(R.string.all_players_left);
		}
			AlertDialog.Builder alt_bld = new AlertDialog.Builder(c);
			alt_bld.setMessage(dialogMessage)
					.setCancelable(false)
					.setPositiveButton(c.getString(android.R.string.ok), positiveListener);
			
			AlertDialog alert = alt_bld.create();
			// Title for AlertDialog
			// alert.setTitle("Exit Game?");
			// Icon for AlertDialog
			alert.setIcon(R.drawable.icon);
			alert.show();
	}
	public static void showLeaveDialog(Context c, DialogInterface.OnClickListener positiveListener,
			DialogInterface.OnClickListener negativeListener) {
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(c);
		alt_bld.setMessage(c.getString(R.string.confirm_leave))
				.setCancelable(false)
				.setPositiveButton(c.getString(R.string.leave), positiveListener)
				.setNegativeButton(c.getString(R.string.keep), negativeListener);
		
		AlertDialog alert = alt_bld.create();
		// Title for AlertDialog
		// alert.setTitle("Exit Game?");
		// Icon for AlertDialog
		alert.setCancelable(true);
		alert.setIcon(R.drawable.icon);
		alert.show();
	}
	public static void hideKeyboard(Context c,View v)
	{
		InputMethodManager imm = (InputMethodManager)c.getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	
	
}
